package app.netant.galgame.bangumi.cartoon;

import java.util.HashSet;
import java.util.Set;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
import cn.lily.com.lang.Basic;
/**
 * bangumi 爬虫 - 明细
 * @author JiaJ
 * @date 2018年6月14日
 *
 */
public class BangumiAntDetail extends NetAntBase {
	
	private BangumiCartoonEntity carEntity;
	
	public void setEntity(BangumiCartoonEntity carEntity){
		this.carEntity = carEntity;
	}

	public BangumiAntDetail(String netSite) {
		super(netSite);
	}
	
	private String getTitle(){
		Elements activePart = document.select("#wrapperNeue #headerSubject .nameSingle a");
		if(activePart.size()>0){	
			return this.getString(activePart.get(0).text());
		}else{
			return "";
		}
	}
	
	private String getTitleType(){
		Elements activePart = document.select("#wrapperNeue #headerSubject .nameSingle small");
		if(activePart.size()>0){
			return this.getString(activePart.get(0).text());
		}else{
			return "";
		}
	}
	
	private void getPros(){
		Elements activePart = document.select("#wrapperNeue .mainWrapper .columns.clearit "
				                            + "#columnSubjectHomeA #bangumiInfo .infobox #infobox li");
		for(int i=0;i<activePart.size();i++){
			System.out.println(activePart.get(i).text());
		}
	}
	
	private String getMemo(){
		Elements activePart = document.select("#wrapperNeue .mainWrapper .columns.clearit "
                                            + "#columnSubjectHomeB #columnSubjectInHomeB #subject_detail #subject_summary");
		if(activePart.size()>0){	
			//System.out.println(this.getString(activePart.get(0).html()));
			return this.getString(activePart.get(0).html());
		}else{
			return "";
		}
		
	}
	
	private String getTags(){
		Elements activePart = document.select("#wrapperNeue .mainWrapper .columns.clearit "
                                            + "#columnSubjectHomeB #columnSubjectInHomeB #subject_detail "
                                            + ".subject_tag_section .inner a span");
		String tags = "";
		for(int i=0;i<activePart.size();i++){
			tags += "," + activePart.get(i).text();
		}
		
		if(!"".equals(tags)){
			return tags.substring(1);
		}
		
		return tags;
	}
	
	public Set<String> extTags(){
		
		Elements activePart = document.select("#wrapperNeue .mainWrapper .columns.clearit "
                + "#columnSubjectHomeA #bangumiInfo .infobox #infobox li");
		
		Set<String> keys = new HashSet<String>();
		
		for(int i=0;i<activePart.size();i++){
			
			String tags = activePart.get(i).text();
			
			String[] tagi = Basic.split(tags, "：",":");
			
			if(tagi.length == 2){
				keys.add(tagi[0]);
			}
			
		}
		
		return keys;
		
	}
	
	private void analyseTags(){
		
		Elements activePart = document.select("#wrapperNeue .mainWrapper .columns.clearit "
                + "#columnSubjectHomeA #bangumiInfo .infobox #infobox li");
		
		for(int i=0;i<activePart.size();i++){
			
			String tags = activePart.get(i).text();
			
			int idx = tags.indexOf(":");
			
			String[] tagi = Basic.split(tags, "：",":");
			
			if(tagi.length == 2){
				this.carEntity.addTagValue(tagi[0], tagi[1]);
			}else if(idx > 0 && idx < tags.length()-1){
				this.carEntity.addTagValue(tags.substring(0,idx),tags.substring(idx+1));
			}
			
		}
		
	}
	
	public void analyse(){
		this.carEntity.setCarTags(this.getString(this.getTags()));
		
		String gameMemo = this.getString(this.getMemo());
		if(gameMemo.length()>1300){
			gameMemo = gameMemo.substring(0, 1300)+"...";
		}
		this.carEntity.setCarMemo(gameMemo);		
		this.carEntity.setCarName(this.getString(this.getTitle()));
		this.carEntity.setCarType(this.getTitleType());
		this.analyseTags();
		this.carEntity.insert();
	}
	
	public static void main(String[] args) {
		
		System.out.println("开始连接网站......");
		
		BangumiAntDetail ant = new BangumiAntDetail("http://bangumi.tv/subject/19021");
		
		System.out.println("网站连接成功......");
		
		ant.getTitle();		
		ant.getTitleType();
		ant.getPros();
		ant.getMemo();
		ant.getTags();
		
	}

}
